package com.asia5b.a5mall.member.activity

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5bmall.resource.StrCons
import com.asia5b.a5mall.member.adapter.AddressListAdapter
import com.asia5b.a5mall.member.databinding.ActivityAddresslistBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.event.AddressListDataChange
import com.asia5b.a5mall.middleware.event.SelectAddressChange
import com.asia5b.a5mall.middleware.logic.viewmodel.member.AddressListActivityVM
import com.asia5b.a5mall.middleware.model.member.AddressModel
import com.asia5b.a5mall.middleware.utils.*
import kotlinx.android.synthetic.main.activity_addresslist.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by zhangyuncai on 2020/1/8.
 */
@Route(path = ActCons.AddressListActivity)
class AddressListActivity : SimpleActivity<AddressListActivityVM, ActivityAddresslistBinding>() {
    override val viewModelClazz: Class<AddressListActivityVM>
        get() = AddressListActivityVM::class.java

    override fun getDataBinding(): ActivityAddresslistBinding {
        isSelectAddressModel = intent.getBooleanExtra("order", false)
        return ActivityAddresslistBinding.inflate(layoutInflater)
    }

    override var sIsDark = true

    val mDatas = arrayListOf<AddressModel>()
    val mAdapter = AddressListAdapter(mDatas)
    val mRecyclerView by lazy { rv_addresslist }
    var isSelectAddressModel = false
    val emptyView by lazy { newEmptyView(mActivity) }

    override fun initView() {
        if (intent.hasExtra("datas")) {
            val datas = intent.getSerializableExtra("datas") as ArrayList<AddressModel>
            loadAddressResult(datas)
        } else {
            mViewModel.loadAddress()
        }
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)!!
            adapter = mAdapter
            addItemDecoration(newDivider(mActivity))
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            val addressModel = mAdapter.getItem(position) as AddressModel
            push(ActCons.NewAddressActivity, { postcard ->
                postcard.withString("type", StrCons.ADDRESS_EDIT)
                postcard.withSerializable("address", addressModel)
            })
        }
    }

    override fun initEvent() {
        //添加
        bt_addresslist_add.setOnClickListener {
            push(ActCons.NewAddressActivity, { postcard ->
                postcard.withString("type", StrCons.ADDRESS_ADD)
            })
        }


        if (isSelectAddressModel) {

            mAdapter.setOnItemClickListener { adapter, view, position ->
                val addressModel = mAdapter.getItem(position) as AddressModel
                getEventBus().post(SelectAddressChange(addressModel))
                pop()
            }
        }

    }

    override fun startObserver() {
        mViewModel.loadAddressResultLiveData.observe(this, Observer {
            loadAddressResult(it)
        })
    }

    override fun initData() {

    }

    fun loadAddressResult(result: ArrayList<AddressModel>) {
        if (emptyView.parent == null) {
            mAdapter.setEmptyView(emptyView)
        }
        mDatas.clear()
        mDatas.addAll(result)
        mAdapter.notifyDataSetChanged()
    }

    override val isRegisterEventBus: Boolean
        get() = true

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAddressListDataChange(event: AddressListDataChange) {
        mViewModel.loadAddress()
    }

}